﻿using System;
using System.ComponentModel.Composition;
using Contracts;
using System.Collections.Generic;
using System.Reflection;

namespace WpfAppHost
{
    [WpfServiceMetadata("AddinsAddin")]
    public class AddinManagerAddinImpl : IWpfServiceAdmin, IPartImportsSatisfiedNotification
    {
        string _status;
        public event EventHandler StatusUpdated;
        private IWpfHost _host;

        private AddinManager _addinManager;

        AddinsUserControl _optionsUI = new AddinsUserControl();

        public AddinManagerAddinImpl()
        {
            Console.WriteLine("Addins addin starting up!");
        }

        public IWpfHost ServiceHost
        {
            get
            {
                return _host;
            }
            set
            {
                _host = value;
                _optionsUI.ServiceHost = value;
                _addinManager = (_host as MainAppWindow).AddinManager;
            }
        }

        void AddinManager_AddinAvailable(object sender, AddinAvailableEventArgs e)
        {
            Status = "New addin detected (watcher).  Please restart the Utility Runner to load it.";
        }

        public System.Windows.Controls.UserControl OptionsUserControl
        {
            get { return _optionsUI; }
        }

        public string Name
        {
            get { return "Addins"; }
        }

        public Uri AddinIconUri
        {
            get {
                string u = CreateResourceUri("Setup_Install.png");
                return new Uri(u);
            }
        }

        private string CreateResourceUri(string name)
        {
            Assembly a = Assembly.GetExecutingAssembly();
            return string.Format("pack://application:,,,/{0};component/Resources/{1}", a.GetName().Name, name);
        }

        public string Description
        {
            get { return ""; }
        }

        public string Author
        {
            get { return "Arian T. Kulp"; }
        }

        public Version Version
        {
            get { return System.Reflection.Assembly.GetExecutingAssembly().GetName().Version; }
        }

        public Uri AuthorUri
        {
            get { return new Uri("http://www.ariankulp.com/"); }
        }

        public Uri UpdateUri
        {
            get { return new Uri("http://www.ariankulp.com/downloads/"); }
        }

        public string Status
        {
            get { return _status; }
            set
            {
                _status = value;

                if (StatusUpdated != null)
                    StatusUpdated(this, null);
            }
        }

        public void Initialize()
        {
            _optionsUI.DataContext = this;
        }

        public void Start()
        {
            _addinManager.AddinAvailable += new EventHandler<AddinAvailableEventArgs>(AddinManager_AddinAvailable);
        }

        public void Stop()
        {
        }


        #region IPartImportsSatisfiedNotification Members

        public void OnImportsSatisfied()
        {
            ;
        }

        #endregion
    }
}
